package de.hhbk.feedbackmodul.daobeans;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.apache.log4j.Logger;

import de.hhbk.feedbackmodul.daointerfaces.AnswerSheetDaoIF;
import de.hhbk.feedbackmodul.entities.AnswerSheet;

@Stateless
public class AnswerSheetDaoBean implements AnswerSheetDaoIF {

	@PersistenceContext(unitName = "FeedbackModulEJB")
	private EntityManager manager;
	
	private Logger logger = Logger.getLogger(this.getClass());
	

	@SuppressWarnings("unchecked")
	@Override
	public List<AnswerSheet> getAllAnswerSheets() throws Exception  {
		try
		{
			Query query = manager.createQuery("select anss from AnswerSheet anss", AnswerSheet.class);
			return (List<AnswerSheet>) query.getResultList();
		}
		catch(Exception e)
		{
			logger.error("Error while reading AnswerSheets: "+e.getMessage(), e);
			throw e;
		}
	}


	@Override
	public void createAnswerSheet(AnswerSheet answerSheet) throws Exception {
		try
		{
			manager.persist(answerSheet);
		}
		catch(Exception e)
		{
			logger.error("Error while persisting AnswerSheet: "+e.getMessage(), e);
			throw e;
		}
	}


	@Override
	public void updateAnswerSheet(AnswerSheet answerSheet) throws Exception {
		try
		{
			manager.merge(answerSheet);
		}
		catch(Exception e)
		{
			logger.error("Error while updating AnswerSheet: "+e.getMessage(), e);
			throw e;
		}
	}


	@Override
	public void deleteAnswerSheet(AnswerSheet answerSheet) throws Exception {
		try
		{
			AnswerSheet answerSheetToDelete = manager.find(AnswerSheet.class, answerSheet.getId());
			manager.remove(answerSheetToDelete);
		}
		catch(Exception e)
		{
			logger.error("Error while deleting AnswerSheet: "+e.getMessage(), e);
			throw e;
		}
		
	}

}
